/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.easemob.chatuidemo.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.easeui.ui.EaseBaseActivity;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;

import onion.molingo2o.Config;
import onion.molingo2o.R;
import onion.molingo2o.utils.ImageLoaderConfig;
import onion.molingo2o.views.LoadingDialog;

public class BaseActivity extends EaseBaseActivity {
   ImageView ivBack,ivRight;
    TextView tvLeft,tvRight,tvTitle;
    protected LoadingDialog mLdDialog = null;
    private void InitDialog() {
        mLdDialog = new LoadingDialog(this);
        // mLdDialog.setMessage(this.getString(R.string.dialog_loading_msg));
        mLdDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                dialog.cancel();
                return false;
            }
        });
    }
    protected View.OnClickListener getTopClickListener(){
        return  new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        };
    }
    @Override
    protected void onCreate(Bundle arg0) {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (!ImageLoader.getInstance().isInited()) {
            ImageLoaderConfig.initImageLoader(this, Config.BASE_IMAGE_CACHE);
        }
        super.onCreate(arg0);
    }
    protected void initTitle(String left, String title, String right) {
        if(findViewById(R.id.heaptop)==null)return;
        ivBack = (ImageView) findViewById(R.id.iv_back);
        tvLeft = (TextView) findViewById(R.id.tv_left_text);
        tvRight = (TextView) findViewById(R.id.tv_right_text);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        if (TextUtils.isEmpty(left)  ) {
            ivBack.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        } else {
            ivBack.setVisibility(View.GONE);
            tvLeft.setVisibility(View.VISIBLE);
            tvLeft.setText(left);
            tvLeft.setOnClickListener(getTopClickListener());
        }
        if (title != null) {
            tvTitle.setText(title);
        }
        if (right != null&&!right.isEmpty()) {
            tvRight.setText(right);
            tvRight.setOnClickListener(getTopClickListener());
        }
    }
    protected void initTitle(String left, String title, int resId){
        if(findViewById(R.id.heaptop)==null)return;
        ivRight=(ImageView) findViewById(R.id.iv_right);
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(resId);
        ivRight.setOnClickListener(getTopClickListener());
        initTitle(left, title, "");
    }
    protected void initTitle(int leftResId, String title, int resId){
        ivBack = (ImageView) findViewById(R.id.iv_back);
        tvLeft = (TextView) findViewById(R.id.tv_left_text);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        ivRight=(ImageView) findViewById(R.id.iv_right);
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(resId);
        ivBack.setImageResource(leftResId);
        tvTitle.setText(title);
    }
    protected void showToast(int strid) {
        Toast.makeText(this, strid, Toast.LENGTH_SHORT).show();
    }

    protected void showToastLong(int strid) {
        Toast.makeText(this, strid, Toast.LENGTH_LONG).show();
    }
    public  void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
